/*
* $Id: ListBoxStateParser.cpp 92 2010-05-26 00:57:49Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ListBoxStateParser.h"

CListBoxStateParser::CListBoxStateParser(const std::tstring& strState){
	ParseState(strState);
}

void CListBoxStateParser::ParseState(const std::tstring& strState){
	int Pos = (int)strState.find('\n');
	if(Pos == -1){
		return; }
	m_strSel.replace(m_strSel.begin(), m_strSel.end(), strState.c_str(), Pos);
	while(true){
		Pos += 1;
		int PosEnd = (int)strState.find(_T(","), Pos);
		if(PosEnd == -1){
			ParseElement(std::tstring(strState.begin() + Pos, strState.end()));
			break;
		}
		ParseElement(std::tstring(strState.begin() + Pos, strState.begin() + PosEnd));
		Pos = PosEnd;
	}
}

void CListBoxStateParser::ParseElement(const std::tstring& strElement){
	int Pos = 0, PosEnd;
	std::tstring strName, strType, strOperation;
	for (int i = 0; i < 3; i++){
		std::tstring strTemp;
		PosEnd = (int)strElement.find(_T("|"), Pos);
		if(PosEnd == -1 && i < 2){
			return; }
		else if(PosEnd == -1 && i == 2)
			strTemp.append(strElement.begin() + Pos, strElement.end());
		else
			strTemp.append(strElement.begin() + Pos, strElement.begin() + PosEnd);
		Pos = PosEnd + 1;
		if(i == 0)
			strName = strTemp;
		else if(i == 1)
			strType = strTemp;
		else if(i == 2)
			strOperation = strTemp;
	}
	if(strOperation == _ADD)
		m_aAddCols.push_back(Col(strName, _ttoi(strType.c_str()), strOperation));
	if(strOperation == _DELETE)
		m_aDelCols.push_back(Col(strName, _ttoi(strType.c_str()), strOperation));
	if(strOperation == _EXIST)
		m_aExistCols.push_back(Col(strName, _ttoi(strType.c_str()), strOperation));
}

std::vector<Col> CListBoxStateParser::GetAddColumn(){
	return m_aAddCols;
}

std::vector<Col> CListBoxStateParser::GetDeleteColumn(){
	return m_aDelCols;
}

std::vector<Col> CListBoxStateParser::GetExistColumn(){
	return m_aExistCols;
}

std::tstring CListBoxStateParser::GetCurSel(){
	return m_strSel;
}

std::tstring CListBoxStateParser::GetState(){
	std::tstring strState(m_strSel);
	strState = strState + _T("\n");
	std::vector<Col> aCols;
	aCols.insert(aCols.end(), m_aAddCols.begin(), m_aAddCols.end());
	aCols.insert(aCols.end(), m_aDelCols.begin(), m_aDelCols.end());
	aCols.insert(aCols.end(), m_aExistCols.begin(), m_aExistCols.end());
	for (std::vector<Col>::iterator it = aCols.begin(); it != aCols.end(); it++){
		if(it != aCols.begin())
			strState.append(_T(","));
		strState = strState + it->m_strName + _T("|") + CConvertFunction::ConvertFromInt(it->m_nId) + _T("|") + it->m_strOperation;
	}
	return strState;
}

void CListBoxStateParser::SetCurSel(const std::tstring& strState){
	m_strSel = strState;
}

void CListBoxStateParser::AddChange(const std::tstring& strName, int nType, const std::tstring& strOperation){
	if(strOperation == _DELETE){
		for (std::vector<Col>::iterator it = m_aAddCols.begin(); it != m_aAddCols.end(); it++){
			if(it->m_strName == strName){
				m_aAddCols.erase(it);
				return;
			}
		}
	}
	m_aAddCols.push_back(Col(strName, nType, strOperation));
}

void CListBoxStateParser::SaveChange(){
	for (std::vector<Col>::iterator it = m_aDelCols.begin(); it != m_aDelCols.end(); it++){
		std::vector<Col>::iterator it1 = std::find(m_aExistCols.begin(), m_aExistCols.end(), it->m_strName);
		if(it1 != m_aExistCols.end()){
			m_aExistCols.erase(it1);
			continue;
		}
	}
	for (std::vector<Col>::iterator it = m_aAddCols.begin(); it != m_aAddCols.end(); it++){
		it->m_strOperation = _EXIST;
		m_aExistCols.push_back(*it);
	}
	m_aAddCols.clear();
	m_aDelCols.clear();
}

std::vector<Col> CListBoxStateParser::GetAllColumns(){
	std::vector<Col> aAddColumn;
	std::vector<Col> aExistColumn = GetExistColumn();
	aAddColumn.insert(aAddColumn.end(), aExistColumn.begin(), aExistColumn.end());
	std::vector<Col> aDeleteColumn = GetDeleteColumn();
	for (std::vector<Col>::iterator it = aDeleteColumn.begin(); it != aDeleteColumn.end(); it++){
		std::vector<Col>::iterator it1 = std::find(aAddColumn.begin(), aAddColumn.end(), *it);
		if(it1 != aAddColumn.end())
			aAddColumn.erase(it1);
	}
	std::vector<Col> aTemp = GetAddColumn();
	aAddColumn.insert(aAddColumn.end(), aTemp.begin(), aTemp.end());
	return aAddColumn;
}